#ifndef CONNECTIONITEM_H
#define CONNECTIONITEM_H

#include <QGraphicsLineItem>
#include <QPen>

class PlcElement;

class ConnectionItem : public QGraphicsLineItem {
public:
    enum { Type = UserType + 10 };
    enum Side { Left, Right };
    // 元素-元素构造函数
    ConnectionItem(PlcElement *startItem, Side startSide, PlcElement *endItem,   Side endSide, QGraphicsItem *parent = nullptr);
    // 元素-锚点构造函数
    ConnectionItem(PlcElement *startItem, Side startSide, const QPointF& endAnchorScenePos, QGraphicsItem *parent = nullptr);

    int type() const override;
    // 折线模式设置（start -> bend -> end）
    void setPolylineMode(bool on, const QPointF& bendPoint);
    void updatePosition();
    PlcElement *startItem() const;
    PlcElement *endItem() const;
    Side startSide() const;
    Side endSide() const;

    // 序列化所需的只读 getter
    bool isPolyline() const { return m_polyline; }
    QPointF bendPoint() const { return m_bend; }
    QPointF startAnchorScenePos() const { return m_startAnchorScenePos; }
    QPointF endAnchorScenePos() const { return m_endAnchorScenePos; }

    bool startIsAnchor();
    bool endIsAnchor();

protected:
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
    QPainterPath shape() const override;
    QRectF boundingRect() const override;

private:
    PlcElement *m_startItem;
    PlcElement *m_endItem;
    QColor m_color = Qt::black;
    Side m_startSide = Right;
    Side m_endSide = Left;

    // 支持锚点（场景坐标固定点）
    bool m_startIsAnchor = false;
    bool m_endIsAnchor = false;
    QPointF m_startAnchorScenePos;
    QPointF m_endAnchorScenePos;

    // 折线显示支持
    bool m_polyline = false;
    QPointF m_bend;   // 拐点（场景坐标）
};

#endif // CONNECTIONITEM_H
